﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;


namespace Xiaov.SmartWeb.Application.HXZB
{
    public partial class ZyzbRightDownData : System.Web.UI.Page
    {
        string zbbm = "", zbxz = "", dwbm = "", nf = "", yf = "", type = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                #region 获取参数
                if (Request.QueryString["zbbm"] != null)
                {
                    zbbm = Request.QueryString["zbbm"].ToString();
                }
                if (Request.QueryString["zbxz"] != null)
                {
                    zbxz = Request.QueryString["zbxz"].ToString();
                }
                if (Request.QueryString["dwbm"] != null)
                {
                    dwbm = Request.QueryString["dwbm"].ToString();
                }
                if (Request.QueryString["nf"] != null && Request.QueryString["yf"] != null)
                {
                    nf = Request.QueryString["nf"].ToString();
                    yf = Request.QueryString["yf"].ToString();
                }
                if (Request.QueryString["type"] != null)//数据信息 标题，本年数据，去年数据
                {
                    type = Request.QueryString["type"].ToString();
                }
                #endregion
                DataTable dt = null;
                switch (zbbm)
                {
                    #region 工业总产值
                    case "TG030003":
                        dt = InitGyzcz();
                        break;
                    #endregion
                    #region 工业销售产值
                    case "TG030004":
                        dt = InitGyxscz();
                        break;
                    #endregion
                    #region 原油产量
                    case "TG030008":
                        dt = InitYycl();
                        break;
                    #endregion
                    #region 天然气产量
                    case "TG030026":
                        dt = InitTrqcl();
                        break;
                    #endregion
                    #region 利润总额
                    case "TG820013":
                        break;
                    #endregion
                    #region 原油递减率
                    case "TG070031":
                        dt = InitYydjl();
                        break;
                    #endregion
                    #region 能源合计
                    case "NH6111":
                        dt = InitNyhj();
                        break;
                    #endregion
                    default:
                        break;
                }
                #region 输出数据
                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (dt.Rows[i]["bnlj"].ToString() == "")
                        {
                            dt.Rows[i]["bnlj"] = 0;
                        }
                        if (dt.Rows[i]["qntqlj"].ToString() == "")
                        {
                            dt.Rows[i]["qntqlj"] = 0;
                        }
                        if (type == "title")
                        {
                            if (i != dt.Rows.Count - 1)
                            {
                                Response.Write(dt.Rows[i]["yf"].ToString() + "月\t");
                            }
                            else
                            {
                                Response.Write(dt.Rows[i]["yf"].ToString());
                            }
                        }
                        else if (type == "bnlj")
                        {
                            if (i != dt.Rows.Count - 1)
                            {
                                Response.Write(dt.Rows[i]["bnlj"].ToString() + "\t");
                            }
                            else
                            {
                                Response.Write(dt.Rows[i]["bnlj"].ToString());
                            }
                        }
                        else if (type == "qntq")
                        {
                            if (i != dt.Rows.Count - 1)
                            {
                                Response.Write(dt.Rows[i]["qntqlj"].ToString() + "\t");
                            }
                            else
                            {
                                Response.Write(dt.Rows[i]["qntqlj"].ToString());
                            }
                        }
                    }
                    Response.End();
                }
                #endregion               
            }
        }

        #region 工业总产值
        protected DataTable InitGyzcz()
        {
            string zbbm = "TG040020002";
            switch (zbxz)
            {
                case "原油":
                    zbbm = "TG040020002";
                    break;
                case "凝析油":
                    zbbm = "TG040020003";
                    break;
                case "分成原油":
                    zbbm = "TG040020004";
                    break;
                case "合作原油":
                    zbbm = "TG040020005";
                    break;
                case "清罐渣油":
                    zbbm = "TG040020006";
                    break;
                case "天然气":
                    zbbm = "TG040020007";
                    break;
                default:
                    break;
            }
            StringBuilder sbSql = new StringBuilder();
            sbSql.Append("select b.yf,a.xjljcz bnlj,b.xjljcz qntqlj from ");
            sbSql.Append("(select * from tg04_2 t WHERE zbbm='{0}' and nf={1} and yf<={2} and dwbm='{3}') a,");
            sbSql.Append("(select * from tg04_2 t WHERE zbbm='{0}' and nf={1}-1 and yf<={2} and dwbm='{3}') b ");
            sbSql.Append("where a.yf(+)=b.yf ");
            sbSql.Append("order by to_number(b.yf)");
            DataTable dt = Maticsoft.DBUtility.DbHelperOra.Query(string.Format(sbSql.ToString(), zbbm, nf, "12", "HB")).Tables[0];
            return dt;
        }
        #endregion

        #region 工业销售产值
        protected DataTable InitGyxscz()
        {
            string zbbm = "TG040020002";
            switch (zbxz)
            {
                case "原油":
                    zbbm = "TG040020002";
                    break;
                case "凝析油":
                    zbbm = "TG040020003";
                    break;
                case "分成原油":
                    zbbm = "TG040020004";
                    break;
                case "合作原油":
                    zbbm = "TG040020005";
                    break;
                case "清罐渣油":
                    zbbm = "TG040020006";
                    break;
                case "天然气":
                    zbbm = "TG040020007";
                    break;
                default:
                    break;
            }
            StringBuilder sbSql = new StringBuilder();
            sbSql.Append("select b.yf,a.xjljxscz bnlj,b.xjljxscz qntqlj from ");
            sbSql.Append("(select * from tg04_2 t WHERE zbbm='{0}' and nf={1} and yf<={2} and dwbm='{3}') a,");
            sbSql.Append("(select * from tg04_2 t WHERE zbbm='{0}' and nf={1}-1 and yf<={2} and dwbm='{3}') b ");
            sbSql.Append("where a.yf(+)=b.yf ");
            sbSql.Append("order by to_number(b.yf)");
            DataTable dt = Maticsoft.DBUtility.DbHelperOra.Query(string.Format(sbSql.ToString(), zbbm, nf, "12", "HB")).Tables[0];
            return dt;
        }
        #endregion

        #region 原油产量
        protected DataTable InitYycl()
        {
            dwbm = getDwbm(zbxz);
            StringBuilder sbSql = new StringBuilder();
            sbSql.AppendFormat(@"select b.yf,a.bnlj,b.bnlj qntqlj from (select bnlj,yf from sc01 where nf={0} and yf<={1} and zbbm='SC010001' and dwbm='{2}') a,
                (select bnlj,yf from sc01 where nf={0}-1 and yf<={1} and zbbm='SC010001' and dwbm='{2}') b where a.yf(+)=b.yf order by to_number(b.yf)",
                 nf, "12", dwbm);
            DataTable dt = Maticsoft.DBUtility.DbHelperOra.Query(sbSql.ToString()).Tables[0];
            return dt;
        }
        #endregion

        #region 天然气产量
        protected DataTable InitTrqcl()
        {
            StringBuilder sbSql = new StringBuilder();
            sbSql.AppendFormat(@"select b.yf,a.bnlj,b.bnlj qntqlj from (select bnlj,yf from sc01 where nf={0} and yf<={1} and zbbm='SC010003' and dwbm='{2}') a,
                (select bnlj,yf from sc01 where nf={0}-1 and yf<={1} and zbbm='SC010003' and dwbm='{2}') b where a.yf(+)=b.yf order by to_number(b.yf)",
                 nf, "12", dwbm);
            DataTable dt = Maticsoft.DBUtility.DbHelperOra.Query(sbSql.ToString()).Tables[0];
            return dt;
        }
        #endregion

        #region 原油递减率
        protected DataTable InitYydjl()
        {
            StringBuilder sbSql = new StringBuilder();
            if (zbxz == "子项")
            {
                sbSql.Append("select b.yf,a.ljzx bnlj,b.ljzx qntqlj from ");
                sbSql.Append("(select * from tg07 t WHERE zbbm='TG070031' and nf={1} and yf<={0} and dwbm='{2}') a,");
                sbSql.Append("(select * from tg07 t WHERE zbbm='TG070031' and nf={1}-1 and yf<={0} and dwbm='{2}') b ");
                sbSql.Append("where a.yf(+)=b.yf ");
                sbSql.Append("order by to_number(b.yf)");
            }
            else if (zbxz == "母项")
            {
                sbSql.Append("select b.yf,a.ljmx bnlj,b.ljmx qntqlj from ");
                sbSql.Append("(select * from tg07 t WHERE zbbm='TG070031' and nf={1} and yf<={0} and dwbm='{2}') a,");
                sbSql.Append("(select * from tg07 t WHERE zbbm='TG070031' and nf={1}-1 and yf<={0} and dwbm='{2}') b ");
                sbSql.Append("where a.yf(+)=b.yf ");
                sbSql.Append("order by to_number(b.yf)");
            }
            DataTable dt = Maticsoft.DBUtility.DbHelperOra.Query(string.Format(sbSql.ToString(), "12", nf, "HB")).Tables[0];
            return dt;
        }
        #endregion

        #region 能源合计
        protected DataTable InitNyhj()
        {
            StringBuilder sbSql = new StringBuilder();
            switch (zbxz)
            {
                case "天 然 气":
                    zbbm = "NH6104";                   
                    break;
                case "原   油":
                    zbbm = "NH6102";                  
                    break;
                case "汽   油":
                    zbbm = "NH6105";
                    break;
                case "柴   油":
                    zbbm = "NH6106";
                    break;
                case "电   力":
                    zbbm = "NH6101";
                    break;
                default:
                    break;
            }
            sbSql.AppendFormat(@"select b.yf,a.xhhj bnlj,b.xhhj qntqlj from (select * from nh61 t WHERE zbbm='{0}' and nf={1} and yf<={2} and dwbm='{3}') a,
                        (select * from nh61 t WHERE zbbm='{0}' and nf={1}-1 and yf<={2} and dwbm='{3}') b where a.yf(+)=b.yf order by to_number(b.yf)", zbbm, nf, "12", "HB");
            DataTable dt = Maticsoft.DBUtility.DbHelperOra.Query(sbSql.ToString()).Tables[0];
            return dt; 
        }              
        #endregion

        protected string getDwbm(string zbxz)
        {
            string dwbm = "";
            string sql = "select dwbm from dwbmb where dwmc='" + zbxz + "'";
            dwbm = Maticsoft.DBUtility.DbHelperOra.GetSingle(sql).ToString();
            return dwbm;
        }
    }
}